// Global Vars
var VIDEOS = [];
var currentPage = 0;
var slideshowOpen = true;
var lang = "en";

// Load jQuery
google.load("jquery", "1.3.2");

// Gadget Init
google.setOnLoadCallback(function() {
	$('#homead .carousel').hover(function(){
		$('#homead .carousel').animate({ bottom: '0px'}, 100, 'swing', function(){ slideshowOpen = true; });
	}, function(){
		if (slideshowOpen == true)
		{
			$('#homead .carousel').animate({ bottom: '-61px'}, 100, 'swing', function(){ slideshowOpen = false; });
		}
	});

	/*switch(lang)
	{		
		case 'fr':
			_IG_EmbedFlash('/ig/proxy?url=http://homead.googlecode.com/svn/trunk/countdown_where.swf', 'countdown', {wmode: 'transparent', width: '200', flashvars: 'lang=fr'});
			break;
		default:
			_IG_EmbedFlash('/ig/proxy?url=http://homead.googlecode.com/svn/trunk/countdown_where.swf', 'countdown', {wmode: 'transparent', width: '200'});
	}*/

	initVideos();
});

function initVideos()
{
	if (VIDEOS.length < 1)
	{
		// Fetch Data
		$('#loader').fadeIn(200);
		fetchPlaylist(function(response)
		{
			youtube.parsePlaylist(response, function(data)
			{
				VIDEOS = data;
				refreshVideoPaging();
				displayVideos();
			});
		});
	}
}

function changeVideoPage(direction)
{
	var videoWidth = ($('.container ul li').outerWidth(true) * 3);

	switch(direction)
	{
		case 'next':
			currentPage ++;
			break;
		case 'previous':
			currentPage --;
			break;
	}

	$('.container ul').animate({left:'-'+videoWidth*currentPage+'px'}, 200, "swing", function(){
		refreshVideoPaging();
	});
}

function refreshVideoPaging()
{
	var videoCount = VIDEOS.length;
	var videoPages = (Math.ceil(VIDEOS.length / 3) - 1);

	if (currentPage <= 0)
	{
		$('.left').unbind('click').hide();
	}
	else
	{
		$('.left').unbind('click').click(function(){changeVideoPage('previous'); return false;}).show();
	}

	if (currentPage < videoPages)
	{
		$('.right').unbind('click').click(function(){changeVideoPage('next'); return false;}).show();
	}
	else
	{
		$('.right').unbind('click').hide();
	}
}

function playVideo(_vid)
{
	$('#video').show();
	if (typeof(_vid) == 'string')
	{		
		_IG_EmbedFlash(_vid, 'video', {});
	}
	else if (typeof(_vid) == 'object')
	{
		_IG_EmbedFlash(_vid.VideoSwfUrl + '&amp;egm=0&amp;fs=0&amp;showinfo=0', 'video', {});
	}
}

function displayVideos()
{
	$('#loader').fadeOut(200);

	if (VIDEOS.length >= 1)
	{
		// Cycle Videos
		$.each(VIDEOS, function(i, val)
		{	
			var html = '<li><a title="'+val.title+'" href="#"><img height="57" width="75" src="'+val.thumbnail+'"/></a></li>';
						
			$('.carousel .container ul').append(html);
			$('.carousel .container ul li:last').click(function(){ playVideo(val); return false; });
		});

		$('.carousel ul').fadeIn(200);
	}
	else
	{
		$('.error').fadeIn(200);
		
	}
}

// Fetch videos
function fetchPlaylist(callback)
{

	switch(lang)
	{		
		case 'fr':
			var url = 'http://gdata.youtube.com/feeds/api/playlists/7D6DED312ED49F62';
			break;
		default:
			var url = 'http://gdata.youtube.com/feeds/api/playlists/78C3829528837075';
	}	

	_IG_FetchXmlContent(url , function(response){
		if (response != null)
		{
			callback(response);
		}
		else
		{
			callback('error');
		}
	});
}